package com.brush.brushsysapi.service.impl;

import com.brush.brushcommon.cache.PartyGroupConfigCache;
import com.brush.brushcommon.cache.PartyGroupConfigCacheDO;
import com.brush.brushcommon.dto.Result;
import com.brush.brushcommon.utils.OBSUtil;
import com.brush.brushcommon.utils.OSSUtil;
import com.brush.brushsysapi.service.FileUploadService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class fileUploadImpl implements FileUploadService {



    @Autowired
    PartyGroupConfigCache partyGroupConfigCache;


    @Override
    public Result uploadFile(MultipartFile[] myFiles) throws Exception{
        if (myFiles == null || myFiles.length == 0) {
            return Result.fail(500,"上传文件不能为空");
        }
        //文件上传方式 oss，obs，local
        PartyGroupConfigCacheDO fileUploadTypeConfig=partyGroupConfigCache.get("fileUploadType","fileUploadType");
        if (fileUploadTypeConfig == null || StringUtils.isBlank(fileUploadTypeConfig.getConfigValue())) {
            return Result.fail(500,"请通知管理员维护文件上传方式");
        }
        List<String> list = new ArrayList<String>();
        for (MultipartFile myFile : myFiles) {
            // 上传文件 ,
            if (myFile.isEmpty()) {
                log.error("上传文件为空");
                continue;
            } else {
                String path="";
                switch (fileUploadTypeConfig.getConfigValue()){
                    case "obs": path = OBSUtil.uploadFile(myFile); break;
                    case "oss": path = OSSUtil.uploadFile(myFile); break;
                    default: return Result.fail(500,"文件上传方式维护异常");
                }
                list.add(path);
            }
        }
        //替换掉空格、"["、"]"
        String paths = list.toString().replaceAll("[\\s]|[\\[]|[\\]]", "");
        return Result.success(paths);
    }




}
